package cn.shoukaiseki.sjf4j.fastjson;

import cn.shoukaiseki.sjf4j.NumberInput;
import cn.shoukaiseki.sjf4j.node.JsonNodeType;
import cn.shoukaiseki.sjf4j.node.TreeNode;

import java.math.BigDecimal;

public class FastjsonTextNode extends FastjsonValueNode<String> {

    public FastjsonTextNode(String jsonNode) {
        super(jsonNode);
    }

    @Override
    public JsonNodeType getNodeType() {
        return JsonNodeType.STRING;
    }

    @Override
    public int asInt() {
        return NumberInput.parseAsInt(value,0);
    }

    @Override
    public Integer asIntVal() {
        return NumberInput.parseAsInt(value,0);
    }

    @Override
    public long asLong() {
        return NumberInput.parseAsLong(value,0);
    }

    @Override
    public Long asLongVal() {
        return NumberInput.parseAsLong(value,0);
    }

    @Override
    public String textValue() {
        return value;
    }

    @Override
    public double asDouble() {
        return NumberInput.parseAsDouble(value,0D);
    }

    @Override
    public Double asDoubleVal() {
        return NumberInput.parseAsDouble(value,0D);
    }

    @Override
    public boolean asBoolean() {
        if (value != null) {
            String v = value.trim();
            if ("true".equals(v)) {
                return true;
            }
            if ("false".equals(v)) {
                return false;
            }
        }
        return false;
    }

    @Override
    public BigDecimal asDecimalVal() {
        return new BigDecimal(value);
    }
}
